# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=android-tools
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=34.0.4
_tag=${pkgver}
pkgrel=1
pkgdesc='Android platform tools (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='http://tools.android.com/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-go"
             "${MINGW_PACKAGE_PREFIX}-gtest"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/nmeum/android-tools/releases/download/${_tag}/${_realname}-${_tag}.tar.xz"
        "https://github.com/msys2/adbwinapi/releases/download/${pkgver}/adbwinapi.zip"
        "https://github.com/msys2/adbwinapi/releases/download/${pkgver}/adbwinapi_sources.zip"
        "AdbWinApi.def"
        "0001-android-tools-cmake.patch"
        "0002-android-tools-vendor.patch")
noextract=("${_realname}-${_tag}.tar.xz")
sha256sums=('7a22ff9cea81ff4f38f560687858e8f8fb733624412597e3cc1ab0262f8da3a1'
            'd8856c8d3e4315393c4c47cffa491e973932d39efbe8bfdd2a0456d16b9c56cc'
            'e853151874c9a97697219ce3c5f3c89843f7ef4e325c127b3ec002168d30d208'
            'e6fc31c148e120fc69be53e0464434c492ab0eaa0dbc8f18dd3c19eb2dd2577b'
            '7e394b2c0d86bec01c06995a07e7bcf90ff44fe93dc38f5b58c20d7d52d96a65'
            '3c66c9756272a63d4e5b8a13b2d55e6b42bf3f0a297739cf3ea78e8fd7a3881e')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xf "${_realname}-${_tag}.tar.xz" || true

  cd "${srcdir}/${_realname}-${_tag}"
  cp "${srcdir}"/AdbWinApi.def ./vendor/
  cp -r "${srcdir}"/development ./vendor/

  apply_patch_with_msg \
    0001-android-tools-cmake.patch \
    0002-android-tools-vendor.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
      -DCMAKE_C_FLAGS="${CFLAGS}" \
      ../${_realname}-${_tag}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  install -Dm644 "${srcdir}/${_realname}-${_tag}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  if [ "${MSYSTEM}" = "CLANGARM64" ]; then
    cp -r "${srcdir}/arm64"/*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  elif  [ "${CARCH}" = "x86_64" ]; then
    cp -r "${srcdir}/x64"/*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  else
    cp -r "${srcdir}/win32"/*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  fi
}
